<template>
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox
        v-for="item in list"
        :key="item.id"
        :label="item.id"
      >{{ item.name }}</el-checkbox>

    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="$emit('close')">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/setting'
import { getProfileById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      list: [] // 保存角色列表
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    // 当用户点击确定
    async hSubmit() {
      try {
        // 为用户分配角色
        const res = await assignRoles({ id: this.roleId, roleIds: this.roleIds })
        if (res.code !== 10000) this.$message.error(res.message) // 分配失败提示
        // 分配成功提示
        this.$message.success(res.message)
        // 关闭弹层
        this.$emit('close')
      } catch (error) {
        console.log(error)
      }
    },
    // 获取所有角色列表
    async loadRoles() {
      try {
        const res = await getRoles({ pagesize: 100 })
        if (res.code === 10000) {
          console.log('获取所有角色列表', res)
          this.list = res.data.rows
          const info = await getProfileById(this.roleId)
          console.log('当前员工拥有的角色', info)
          // 保存这个员工当前已有的角色
          this.roleIds = info.data.roleIds
        }
      } catch (error) {
        console.log(error)
      }
    }

  }
}
</script>
